
jQuery(document).ready(function() {

	$('#fileCustomer').liteUploader({
		script: 'uploadCustom',
		rules: {
			allowedFileTypes: 'application/txt',
			maxSize: 8388608
		}
	})
	.on('lu:errors', function (e, errors) {
		var isErrors = false;
		$('#display').html('');

		$.each(errors, function (i, error) {
			if (error.errors.length > 0) {
				isErrors = true;
				$.each(error.errors, function (i, errorInfo) {
					$('#display').append('<br />ERROR! File: ' + error.name + ' - Info: ' + JSON.stringify(errorInfo));
				});
			}
		});

		if (!isErrors) {
			$('#display').append('<br />No errors');
		}
	})
	.on('lu:before', function (e, files) {
		$('#display').append('<br />Uploading ' + files.length + ' file(s)...');
	})
	.on('lu:progress', function (e, percentage) {
		$('#display').append('<br />Progress ' + percentage + '%<br />');
	})
	.on('lu:success', function (e, response) {
		
		$.ajax({
			url : 'getRequirements', // action to be perform
			dataType : 'html',
			success : function(html) {
				$('#requirements').html(html); // set result.jsp output to leftDiv
			},
			error : function(xhr, ajaxOptions, thrownError) {
				alert('An error occurred! ' + thrownError);
			},
		});
		
		llamadoLog();
//		
//		var response = $.parseJSON(response);
//		$('#previews').html(response);
//		
//
//		$.each(response.urls, function(i, url) {
//			$('#previews').append($('<img>', {'src': url, 'width': 200}));
//		});
//
//		$('#display').append(response.message);
	});
	
	
	
	
//	file RFC
	$('#fileRFC').liteUploader({
		script: 'uploadRFC',
		rules: {
			allowedFileTypes: 'application/txt',
			maxSize: 8388608
		}
	})
	.on('lu:errors', function (e, errors) {
		var isErrors = false;
		$('#display').html('');

		$.each(errors, function (i, error) {
			if (error.errors.length > 0) {
				isErrors = true;
				$.each(error.errors, function (i, errorInfo) {
					$('#display').append('<br />ERROR! File: ' + error.name + ' - Info: ' + JSON.stringify(errorInfo));
				});
			}
		});

		if (!isErrors) {
			$('#display').append('<br />No errors');
		}
	})
	.on('lu:before', function (e, files) {
		$('#display').append('<br />Uploading ' + files.length + ' file(s)...');
	})
	.on('lu:progress', function (e, percentage) {
		$('#display').append('<br />Progress ' + percentage + '%<br />');
	})
	.on('lu:success', function (e, response) {
		
		$.ajax({
			url : 'getRequirements', // action to be perform
			dataType : 'html',
			success : function(html) {
				$('#requirements').html(html); // set result.jsp output to leftDiv
			},
			error : function(xhr, ajaxOptions, thrownError) {
				alert('An error occurred! ' + thrownError);
			},
		});
		
		llamadoLog();
//		
//		var response = $.parseJSON(response);
//		$('#previews').html(response);
//		
//
//		$.each(response.urls, function(i, url) {
//			$('#previews').append($('<img>', {'src': url, 'width': 200}));
//		});
//
//		$('#display').append(response.message);
	});
	
	
	
	
//	file R1,R2
	$('#fileR1R2').liteUploader({
		script: 'uploadR1R2',
		rules: {
			allowedFileTypes: 'application/txt',
			maxSize: 8388608
		}
	})
	.on('lu:errors', function (e, errors) {
		var isErrors = false;
		$('#display').html('');

		$.each(errors, function (i, error) {
			if (error.errors.length > 0) {
				isErrors = true;
				$.each(error.errors, function (i, errorInfo) {
					$('#display').append('<br />ERROR! File: ' + error.name + ' - Info: ' + JSON.stringify(errorInfo));
				});
			}
		});

		if (!isErrors) {
			$('#display').append('<br />No errors');
		}
	})
	.on('lu:before', function (e, files) {
		$('#display').append('<br />Uploading ' + files.length + ' file(s)...');
	})
	.on('lu:progress', function (e, percentage) {
		$('#display').append('<br />Progress ' + percentage + '%<br />');
	})
	.on('lu:success', function (e, response) {
		
		$.ajax({
			url : 'getRequirements', // action to be perform
			dataType : 'html',
			success : function(html) {
				$('#requirements').html(html); // set result.jsp output to leftDiv
			},
			error : function(xhr, ajaxOptions, thrownError) {
				alert('An error occurred! ' + thrownError);
			},
		});
		
		llamadoLog();
//		
//		var response = $.parseJSON(response);
//		$('#previews').html(response);
//		
//
//		$.each(response.urls, function(i, url) {
//			$('#previews').append($('<img>', {'src': url, 'width': 200}));
//		});
//
//		$('#display').append(response.message);
	});
	
	
	
//	file Sales Call
	$('#fileSalesCall').liteUploader({
		script: 'uploadSalesCall',
		rules: {
			allowedFileTypes: 'application/txt',
			maxSize: 8388608
		}
	})
	.on('lu:errors', function (e, errors) {
		var isErrors = false;
		$('#display').html('');

		$.each(errors, function (i, error) {
			if (error.errors.length > 0) {
				isErrors = true;
				$.each(error.errors, function (i, errorInfo) {
					$('#display').append('<br />ERROR! File: ' + error.name + ' - Info: ' + JSON.stringify(errorInfo));
				});
			}
		});

		if (!isErrors) {
			$('#display').append('<br />No errors');
		}
	})
	.on('lu:before', function (e, files) {
		$('#display').append('<br />Uploading ' + files.length + ' file(s)...');
	})
	.on('lu:progress', function (e, percentage) {
		$('#display').append('<br />Progress ' + percentage + '%<br />');
	})
	.on('lu:success', function (e, response) {
		
		$.ajax({
			url : 'getRequirements', // action to be perform
			dataType : 'html',
			success : function(html) {
				$('#requirements').html(html); // set result.jsp output to leftDiv
			},
			error : function(xhr, ajaxOptions, thrownError) {
				alert('An error occurred! ' + thrownError);
			},
		});
		
		llamadoLog();
//		
//		var response = $.parseJSON(response);
//		$('#previews').html(response);
//		
//
//		$.each(response.urls, function(i, url) {
//			$('#previews').append($('<img>', {'src': url, 'width': 200}));
//		});
//
//		$('#display').append(response.message);
	});
	
	
	
	
	
//	file Customer Quo
	$('#fileCustomerQuo').liteUploader({
		script: 'uploadCustomerQuotation',
		rules: {
			allowedFileTypes: 'application/txt',
			maxSize: 8388608
		}
	})
	.on('lu:errors', function (e, errors) {
		var isErrors = false;
		$('#display').html('');

		$.each(errors, function (i, error) {
			if (error.errors.length > 0) {
				isErrors = true;
				$.each(error.errors, function (i, errorInfo) {
					$('#display').append('<br />ERROR! File: ' + error.name + ' - Info: ' + JSON.stringify(errorInfo));
				});
			}
		});

		if (!isErrors) {
			$('#display').append('<br />No errors');
		}
	})
	.on('lu:before', function (e, files) {
		$('#display').append('<br />Uploading ' + files.length + ' file(s)...');
	})
	.on('lu:progress', function (e, percentage) {
		$('#display').append('<br />Progress ' + percentage + '%<br />');
	})
	.on('lu:success', function (e, response) {
		
		$.ajax({
			url : 'getRequirements', // action to be perform
			dataType : 'html',
			success : function(html) {
				$('#requirements').html(html); // set result.jsp output to leftDiv
			},
			error : function(xhr, ajaxOptions, thrownError) {
				alert('An error occurred! ' + thrownError);
			},
		});
		
		llamadoLog();
//		
//		var response = $.parseJSON(response);
//		$('#previews').html(response);
//		
//
//		$.each(response.urls, function(i, url) {
//			$('#previews').append($('<img>', {'src': url, 'width': 200}));
//		});
//
//		$('#display').append(response.message);
	});
	
	
	
	
	
	$('#saveAllVoBo').click(function() {
		$.ajax({
			url : 'savevobo', // action to be perform
			dataType : 'html',
			data : {
				box1 : $("#box1").prop('checked'),
				box2 : $("#box2").prop('checked'),
				box3 : $("#box3").prop('checked'),
				box4 : $("#box4").prop('checked'),
				box5 : $("#box5").prop('checked'),
				box6 : $("#box6").prop('checked'),
				box7 : $("#box7").prop('checked'),
				box8 : $("#box8").prop('checked'),
				rateTrackerText : $("#ratetrackertextidform").val(),
				acondeborNumberText : $("#acondebtornumbertextidform").val()

			},
			success : function(html) {
				llamadoLog();
				$('#requirements').html(html); // set result.jsp output to leftDiv
			},
			error : function(xhr, ajaxOptions, thrownError) {
				alert('An error occurred! ' + thrownError);
			},
		});
	});

});

var llamadoLog = function() {

	$.ajax({
		url : 'getLog', // action to be perform
		dataType : 'html',
		success : function(html) {
			$('#log').html(html); // set result.jsp output to leftDiv
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
};

var saveTxtFiles = function() {

	$.ajax({
		url : 'saveTxtFiles', // action to be perform
		data : {
			rateTrackerText : $("#submit1").val(),
			acondeborNumberText : $("#submit2").val()

		},
		dataType : 'html',
		success : function(html) {
			$('#requirements').html(html); // set result.jsp output to leftDiv
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
};



var ponerValorCustomerProfileFile = function() {
	
	$("#valCustomerProfileFile").val($("#fileToUpload").val());

};

function ajaxFileUpload() {
	/* $("#loading")
	.ajaxStart(function(){
	$(this).show();
	})
	.ajaxComplete(function(){
	$(this).hide();
	});*/
	$.ajaxFileUpload({
		url : 'resultAction',
		secureuri : false,
		fileElementId : 'fileToUpload',
		dataType : 'json',
		beforeSend : function() {
			$("#loading").show();
		},
		complete : function() {
			$("#loading").hide();
		},
		success : function(data, status) {
			alert("yea");
			if (typeof (data.error) != 'undefined') {
				if (data.error != '') {
					alert(data.error);
				} else {
					alert(data.msg);
				}
			}
		},
		error : function(data, status, e) {
			alert(e);
		}
	})
	return false;
};

